//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  Board_iPhone_Login2.m
//  DingDing
//
//  Created by LHL on 14/11/21.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "Board_iPhone_Login2.h"
#import "MainNearBy_Board.h"
#import "AddPet.h"
#import "ForgetPassWord.h"
#import "ForgetPassWordFinal.h"
#pragma mark -

@interface Board_iPhone_Login2()
{
	//<#@private var#>
}
@end

@implementation Board_iPhone_Login2

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_OUTLET(BeeUITextField, username)
DEF_OUTLET(BeeUITextField, password)
DEF_OUTLET(BeeUITextField, login)
DEF_OUTLET(BeeUITextField, forgetPwd)
DEF_OUTLET(BeeUIButton, pop)
DEF_MODEL(phoneLogin, iphoneLogin)
- (void)load
{
    self.iphoneLogin = [phoneLogin modelWithObserver:self];
}

- (void)unload
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}


#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.navigationBarShown = NO;
    self.username.placeholder = @" 用户名";
    self.password.placeholder = @" 密码";
    self.forgetPwd.titleTextAlignment = NSTextAlignmentRight;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)KeyboardWillShow:(NSNotification *)aNote{
    NSDictionary *info = [aNote userInfo];
    CGRect inputFrame;
    CGSize keyboardSize = [[info objectForKey:@"UIKeyboardFrameEndUserInfoKey"]CGRectValue].size;
    if (self.username.isEditing) {
        inputFrame  = self.username.frame;
    }else if (self.password.isEditing){
        inputFrame  = self.password.frame;
    }
    if (inputFrame.origin.y + keyboardSize.height+inputFrame.size.height >[UIScreen mainScreen].bounds.size.height) {
        CGRect frame = [UIScreen mainScreen].bounds;
        frame.origin.y = frame.origin.y-inputFrame.size.height-10;
        [UIView animateWithDuration:0.25 animations:^{
            self.view.frame = frame;
        }];
    }
}

-(void)KeyboardWillHide:(NSNotification *)aNote{
    CGRect frame = self.view.frame;
    if (IOS7_OR_LATER) {
        frame.origin.y = 0;
    }else{
        frame.origin.y = 64;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.view.frame = frame;
    }];
}


ON_DELETE_VIEWS( signal )
{
    
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    self.navigationBarShown = NO;
}

ON_DID_APPEAR( signal )
{
//    self.navigationBarShown = NO;
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
    
}

ON_SIGNAL3(phoneLogin, RELOADED, SIGNAL){
    [self dismissTips];
    if ([self.iphoneLogin.code isEqualToString:@"200"]) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isaddpet"]isEqualToString:@"0"]) {
            AddPet *add = [[AddPet alloc]init];
            [self.navigationController pushViewController:add animated:YES];
            return;
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginsuccess" object:nil];
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self presentMessageTips:self.iphoneLogin.message];
    }
}

ON_SIGNAL3(phoneLogin, RELOADING, SIGNAL){
    [self presentLoadingTips:@"登录中"];
}

ON_SIGNAL3(phoneLogin, FAILED, SIGNAL){
    [self dismissTips];
    [self presentMessageTips:@"网络错误"];
}

ON_SIGNAL3(BeeUIButton, login, SIGNAL){
    if (self.username.text.length == 0) {
        [self presentMessageTips:@"用户名不能为空"];
        return;
    }
    if (self.password.text.length == 0 ) {
        [self presentMessageTips:@"密码不能为空"];
        return;
    }
    self.iphoneLogin.phone = self.username.text;
    self.iphoneLogin.password = self.password.text;
    [self.iphoneLogin firstPage];
}



ON_SIGNAL3(BeeUIButton, pop, SIGNAL){
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for (BeeUITextField *t  in self.view.subviews) {
        [t resignFirstResponder];
    }
}

ON_SIGNAL3(BeeUIButton, forgetPwd, SIGNAL){
    ForgetPassWord *pwd = [[ForgetPassWord alloc]init];
    [self.navigationController pushViewController:pwd animated:YES];
}

ON_SIGNAL3( BeeUITextField, RETURN, signal ){
    UITextField *input = (UITextField *)signal.source;
    if (UIReturnKeyDone == input.returnKeyType ) {
        [input resignFirstResponder];
        
    }
}

@end
